/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "MainWindow.h"

#include "ProjectManager.h"

#include "PropertyWindow.h"
#include "HierachyWindow.h"
#include "ObjectWindow.h"
#include "AssetWindow.h"
#include "BuilderApplication.h"


namespace PQBuilder
{
	const QString MainWindow::HIERACHY_WIDGET_NAME="Heriachy";
	const QString MainWindow::OBJECT_CREATE_WIDGET_NAME="Object";
	const QString MainWindow::ASSET_WIDGET_NAME="Asset";

	MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
		: QMainWindow(parent, flags),_projectDialog(0),_fractalTerrainDialog(0),_voronoiDiagramDialog(0)
	{
		
		createMenus();

		this->setWindowTitle(ApplicationSingleton.APPLICATION_NAME);
		this->showMaximized();

	}

	MainWindow::~MainWindow()
	{

	}

	void MainWindow::createMenus()
	{
		QMenuBar* menuBar=this->menuBar();

		QMenu* menuFile = new QMenu(QObject::tr("File"), this);
		QAction* createAction= menuFile->addAction(QObject::tr("New Project"));
		QAction* openAction= menuFile->addAction(QObject::tr("Open Project"));
		QAction* saveAction=menuFile->addAction(QObject::tr("Save"));
		menuBar->addMenu(menuFile);

		QMenu* terrainMenu = new QMenu(QObject::tr("Terrain"), this);
		QAction* createTerrainAction= terrainMenu->addAction(QObject::tr("Create flat terrain"));
		QMenu* terrainGenerateMenu= terrainMenu->addMenu(QObject::tr("Generate terrain"));

		QAction* fractalAction=terrainGenerateMenu->addAction(QObject::tr("Mid Displacement"));
		QAction* voronoiAction=terrainGenerateMenu->addAction(QObject::tr("Voronoi Diagram"));

		menuBar->addMenu(terrainMenu);

		QMenu* materialMenu = new QMenu(QObject::tr("Material"), this);
		QAction* materialEditorAction= materialMenu->addAction(QObject::tr("Open material editor"));
		menuBar->addMenu(materialMenu);

		QMenu* menuHelp = new QMenu(QObject::tr("Help"), this);
		QAction* docAction= menuHelp->addAction(QObject::tr("Doc"));
		QAction* wikiAction= menuHelp->addAction(QObject::tr("Wiki"));
		QMenu* langMenu=menuHelp->addMenu(QObject::tr("Language"));
		QAction* aboutAction= menuHelp->addAction(QObject::tr("About"));
		menuBar->addMenu(menuHelp);

		langMenu->addAction(QObject::tr("Chinese"));
		langMenu->addAction(QObject::tr("English"));

		setMenuBar(menuBar);
		connect(createAction, SIGNAL(triggered()), this, SLOT(selectCreateDialog()));
		connect(openAction, SIGNAL(triggered()), this, SLOT(selectOpenDialog()));
		connect(saveAction, SIGNAL(triggered()), this, SLOT(saveProject()));
		connect(fractalAction, SIGNAL(triggered()), this, SLOT(generateFractalTerrain()));
		connect(voronoiAction,SIGNAL(triggered()),this,SLOT(generateVoronoiDiagram()));

		QShortcut* openShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this);
		QShortcut* createShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_N), this);
		connect(createShortcut, SIGNAL(activated()), this, SLOT(selectCreateDialog()));
		connect(openShortcut, SIGNAL(activated()), this, SLOT(selectOpenDialog()));
	}


	void MainWindow::createDefaultUI()
	{
		createObjectDock();
		createHierachyDock();
		createAssetsDock();
		
		createPropertyDock();
		createSceneWindow();
		
		tabifyDockWidget(_dockAssets,_dockHierachy);
		tabifyDockWidget(_dockAssets,_dockObject);//left will be current
	}

	void MainWindow::createHierachyDock()
	{
		HierachyWindow* widget=new HierachyWindow(this);
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Hierachy"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		dockWidget->setWidget(widget);
		
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

		_dockHierachy=dockWidget;
	}

	void MainWindow::createObjectDock()
	{
		ObjectWindow* widget=new ObjectWindow(this);
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Object"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		dockWidget->setWidget(widget);
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
		
		_dockObject=dockWidget;
	}


	void MainWindow::createAssetsDock()
	{
		AssetWindow* widget=new AssetWindow(this);
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Assets"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		dockWidget->setWidget(widget);
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

		_dockAssets=dockWidget;
	}

	void MainWindow::createPropertyDock()
	{
		PropertyWindow* widget=new PropertyWindow(this);
		
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Property"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		dockWidget->setWidget(widget);
		addDockWidget(Qt::RightDockWidgetArea, dockWidget);

		_dockProperty=dockWidget;
	}

	

	void MainWindow::saveProject()
	{
		
	}


	void MainWindow::generateFractalTerrain()
	{
		if(!_fractalTerrainDialog){
			_fractalTerrainDialog=new FractalTerrainGenerateDialog(this);
		}
		_fractalTerrainDialog->show();
	}

	void MainWindow::generateVoronoiDiagram()
	{
		if(!_voronoiDiagramDialog){
			_voronoiDiagramDialog=new VoronoiDiagramDialog(this);
		}
		_voronoiDiagramDialog->show();

	}


	void MainWindow::selectCreateDialog()
	{
		if(!_projectDialog){
			_projectDialog=new ProjectDialog(this);
		}
		_projectDialog->selectCreateTab();
	}

	void MainWindow::selectOpenDialog()
	{
		if(!_projectDialog){
			_projectDialog=new ProjectDialog(this);
		}
		
		_projectDialog->selectOpenTab();
		_projectDialog->show();
	}


	void MainWindow::createSceneWindow()
	{
		QTabWidget* tab=new QTabWidget(this);
		this->setCentralWidget(tab);
		
		_builderWindow=new SceneWindow(this);
		

		tab->addTab(_builderWindow,QObject::tr("Scene"));

	}

}
